package com.ruoyi.wzgl.wzsq.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.wzgl.wzsq.domain.Wzsq;
import com.ruoyi.wzgl.wzsq.domain.WzsqSqmx;
import com.ruoyi.wzgl.wzsq.mapper.WzsqMapper;
import com.ruoyi.wzgl.wzsq.service.IWzsqService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 物资申请Service业务层处理
 *
 * @author huacai
 * @date 2024-09-29
 */
@Service
public class WzsqServiceImpl implements IWzsqService {
    @Resource
    private WzsqMapper wzsqMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询物资申请
     *
     * @param wzsqId 物资申请主键
     * @return 物资申请
     */
    @Override
    public Wzsq selectWzsqByWzsqId(Integer wzsqId) {
        return wzsqMapper.selectWzsqByWzsqId(wzsqId);
    }

    /**
     * 查询物资申请列表
     *
     * @param wzsq 物资申请
     * @return 物资申请
     */
    @Override
    @DataScope(deptAlias = "t1")
    public List<Wzsq> selectWzsqList(Wzsq wzsq) {
        return wzsqMapper.selectWzsqList(wzsq);
    }

    /**
     * 新增物资申请
     *
     * @param wzsq 物资申请
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWzsq(Wzsq wzsq) {
        wzsq.setCreateTime(DateUtils.getNowDate());
        int rows = wzsqMapper.insertWzsq(wzsq);
        insertWzsqSqmx(wzsq);
        return rows;
    }

    /**
     * 批量新增物资申请
     *
     * @param wzsqs 物资申请List
     * @return 结果
     */
    @Override
    public int batchInsertWzsq(List<Wzsq> wzsqs) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(wzsqs)) {
            try {
                for (int i = 0; i < wzsqs.size(); i++) {
                    int row = wzsqMapper.insertWzsq(wzsqs.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == wzsqs.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改物资申请
     *
     * @param wzsq 物资申请
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWzsq(Wzsq wzsq) {
        wzsq.setUpdateTime(DateUtils.getNowDate());
        wzsqMapper.deleteWzsqSqmxByWzsqId(wzsq.getWzsqId());
        insertWzsqSqmx(wzsq);
        return wzsqMapper.updateWzsq(wzsq);
    }

    /**
     * 批量删除物资申请
     *
     * @param wzsqIds 需要删除的物资申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWzsqByWzsqIds(Integer[] wzsqIds) {
        wzsqMapper.deleteWzsqSqmxByWzsqIds(wzsqIds);
        return wzsqMapper.deleteWzsqByWzsqIds(wzsqIds);
    }

    /**
     * 删除物资申请信息
     *
     * @param wzsqId 物资申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWzsqByWzsqId(Integer wzsqId) {
        wzsqMapper.deleteWzsqSqmxByWzsqId(wzsqId);
        return wzsqMapper.deleteWzsqByWzsqId(wzsqId);
    }

    /**
     * 修改物资申请明细
     *
     * @param wzsqSqmx 物资申请明细
     * @return 结果
     */
    @Override
    @Transactional
    public int updateWzsqSqmx(WzsqSqmx wzsqSqmx) {
        return wzsqMapper.updateWzsqSqmx(wzsqSqmx);
    }

    /**
     * 新增物资申请明细信息
     *
     * @param wzsq 物资申请对象
     */
    public void insertWzsqSqmx(Wzsq wzsq) {
        List<WzsqSqmx> wzsqSqmxList = wzsq.getWzsqSqmxList();
        Integer wzsqId = wzsq.getWzsqId();
        if (StringUtils.isNotNull(wzsqSqmxList)) {
            List<WzsqSqmx> list = new ArrayList<WzsqSqmx>();
            for (WzsqSqmx wzsqSqmx : wzsqSqmxList) {
                wzsqSqmx.setWzsqId(wzsqId);
                list.add(wzsqSqmx);
            }
            if (list.size() > 0) {
                wzsqMapper.batchWzsqSqmx(list);
            }
        }
    }
}
